package com.main.ok.guava;

import com.google.common.util.concurrent.*;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by kai on 2015/6/12 0012.
 */
public class FutureTask {
    public static void main(String[] args) {
        ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
        ListenableFuture<Integer> submit = null;
        for (int i = 0; i < 10; i++) {
            submit = listeningExecutorService.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    TimeUnit.SECONDS.sleep(3);
                    return 7;
                }
            });
            //获取futeure
            Futures.addCallback(submit, new FutureCallback<Integer>() {
                @Override
                public void onSuccess(Integer integer) {
                    System.out.println("execute over,get result:" + integer);
                }

                @Override
                public void onFailure(Throwable throwable) {
                    System.out.println("runs error...");
                }
            });
        }
        System.out.println("------------------");


    }
}

